import path from "path";
import fs from "fs";

import ts from "rollup-plugin-typescript2";
import cjs from "@rollup/plugin-commonjs";

// 所有包的路径
const pkgPath = path.resolve(__dirname, "../../packages");
// 打包产物的
const distPath = path.resolve(__dirname, "../../dist/node_modules");

// 解析 包的路径
export function resolvePkgPath(pkgName, isDist) {
  if (isDist) {
    return `${distPath}/${pkgName}`;
  }
  return `${pkgPath}/${pkgName}`;
}

// 获取 package.json
export function getPackageJSON(pkgName) {
  // 获取包名下的 package.json 下的路径
  const path = `${resolvePkgPath(pkgName)}/package.json`;
  // 读取该文件
  const str = fs.readFileSync(path, { encoding: "utf-8" });

  // 将读取的文件序列化
  return JSON.parse(str);
}

// 获取通用的 plugins
export const getBaseRollupPlugins = ({ typescript = {} } = {}) => {
  console.log(typescript);

  return [
    // 解析 common.js
    cjs(),
    ts(typescript),
  ];
};
